package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bifang.assistant.common.enums.ExcelTypeEnum;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.common.vo.dict.DictQueryVO;
import com.bifang.module.base.common.vo.dict.SelectVO;
import com.bifang.module.base.common.vo.excel.*;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.base.core.helper.ExcelCommonHelper;
import com.bifang.module.base.core.integration.ExcelIntegration;
import com.bifang.module.system.core.dao.model.ExcelModel;
import com.bifang.module.system.core.dao.model.ExcelParam;
import com.bifang.module.system.core.dao.model.SysOrg;
import com.bifang.module.system.core.dao.service.ExcelModelService;
import com.bifang.module.system.core.dao.service.ExcelParamService;
import com.bifang.module.system.core.dao.service.SysDictService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.SchoolCategoryEnum;
import com.jwsoft.manager.common.vo.eduStudentEnrollWhite.WhiteExcelSaveVO;
import com.jwsoft.manager.core.dao.model.*;
import com.jwsoft.manager.core.dao.service.EduSchoolService;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollPreService;
import com.jwsoft.manager.core.util.EduEnrollClearCacheHelper;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.convert.Convert;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

import com.bifang.engine.assistant.core.exception.AppException;
import org.springframework.util.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.jwsoft.manager.common.vo.eduStudentEnrollWhite.EduStudentEnrollWhiteVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollWhite.EduStudentEnrollWhiteKeyVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollWhite.EduStudentEnrollWhiteQueryVO;
import com.jwsoft.manager.core.integration.EduStudentEnrollWhiteIntegration;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollWhiteService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 报名白名单管理业务实现类
 *
 * @author chenzh
 * @since 2024-05-18
 */
@ApiService(funcCode = "eduStudentEnrollWhite", title = "报名白名单管理")
public class EduStudentEnrollWhiteIntegrationImpl implements EduStudentEnrollWhiteIntegration {

    @Autowired
    public EduStudentEnrollWhiteService eduStudentEnrollWhiteService;
    @Autowired
    private EduHelper eduHelper;
    @Autowired
    private EduEnrollClearCacheHelper eduEnrollClearCacheHelper;
    @Autowired
    private EduStudentEnrollPreService enrollPreService;
    @Autowired
    public ExcelIntegration excelIntegration;

    @Autowired
    public ExcelCommonHelper excelCommonHelper;

    @Autowired
    public ExcelModelService excelModelService;

    @Autowired
    public ExcelParamService excelParamService;

    @Autowired
    public SysDictService sysDictService;

    @Autowired
    public EduSchoolService eduSchoolService;
    @Override
    @OpApi(funcCode = "eduStudentEnrollWhite0001", title = "报名白名单管理分页查询", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    public PageInfo<EduStudentEnrollWhiteVO> getList(EduStudentEnrollWhiteQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        //行政区划为空，必须传入行政区划
        if (StringUtils.isBlank(vo.getAreaCode())) {
            vo.setAreaCode(SessionUtil.getAreaCode());
        }
        if (eduHelper.thisAreaCode().equalsIgnoreCase(vo.getAreaCode())){
            vo.setAreaCode(null);
        }
        if (vo.getYear() == null) {
            vo.setYear(eduHelper.thisYear());
        }
        QueryWrapper<EduStudentEnrollWhite> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EduStudentEnrollWhite::getYear,vo.getYear())
                .eq(StringUtils.isNotBlank(vo.getAreaCode()),EduStudentEnrollWhite::getAreaCode,vo.getAreaCode())
                .eq(StringUtils.isNotBlank(vo.getIdcard()),EduStudentEnrollWhite::getIdcard,vo.getIdcard())
                .like(StringUtils.isNotBlank(vo.getFullName()),EduStudentEnrollWhite::getFullName,vo.getFullName())
                .eq(StringUtils.isNotBlank(vo.getSchoolId()),EduStudentEnrollWhite::getSchoolId,vo.getSchoolId())
                .eq(StringUtils.isNotBlank(vo.getAuthStatus()),EduStudentEnrollWhite::getAuthStatus,vo.getAuthStatus())
                .like(StringUtils.isNotBlank(vo.getSchoolName()),EduStudentEnrollWhite::getSchoolName,vo.getSchoolName())
                .eq(StringUtils.isNotBlank(vo.getSchoolId()),EduStudentEnrollWhite::getSchoolId,vo.getSchoolId());
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<EduStudentEnrollWhite> list = eduStudentEnrollWhiteService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduStudentEnrollWhite> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduStudentEnrollWhiteVO> resultList = Convert.toList(EduStudentEnrollWhiteVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollWhite0002", title = "报名白名单管理根据主键查询详情", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    public EduStudentEnrollWhiteVO getById(EduStudentEnrollWhiteKeyVO vo) {
        EduStudentEnrollWhite entity = eduStudentEnrollWhiteService.getById(vo.getEnrollWhiteId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduStudentEnrollWhiteVO result = Convert.convert(EduStudentEnrollWhiteVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "eduStudentEnrollWhite0003", title = "报名白名单管理保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduStudentEnrollWhiteVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
        if (StringUtils.isBlank(vo.getIdcard())) {
            throw new AppException("身份证不能为空");
        }
        if (StringUtils.isBlank(vo.getFullName())) {
            throw new AppException("姓名不能为空");
        }
        if (StringUtils.isBlank(vo.getAreaCode())) {
            throw new AppException("行政区划代码不能为空");
        }
        if (ObjectUtils.isEmpty(vo.getYear())) {
            vo.setYear(eduHelper.thisYear());
        }
        if (StringUtils.isBlank(vo.getSchoolId())) {
            throw new AppException("学校id不能为空");
        }
        if (StringUtils.isBlank(vo.getSchoolName())) {
            throw new AppException("学校名称不能为空");
        }
        String idcard = vo.getIdcard().trim();
        vo.setIdcard(idcard);
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollWhite0004", title = "报名白名单管理新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduStudentEnrollWhite0004",
            operationName = "报名白名单管理新增",
            dataType = "enrollWhiteId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentEnrollWhiteVO add(EduStudentEnrollWhiteVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        long count = eduStudentEnrollWhiteService.count(Wrappers.<EduStudentEnrollWhite>lambdaQuery()
                .eq(EduStudentEnrollWhite::getYear, vo.getYear())
                .eq(EduStudentEnrollWhite::getSchoolId, vo.getSchoolId())
                .eq(EduStudentEnrollWhite::getIdcard, vo.getIdcard()));
        if (count > 0) {
            throw new AppException("该学生已经在白名单了，无法进行添加");
        }
        EduStudentEnrollWhite entity = Convert.convert(EduStudentEnrollWhite.class, vo);
        eduStudentEnrollWhiteService.save(entity);
        clearPreBeforeCache(vo);
        //将主键设置回vo使操作日志可以取到
        vo.setEnrollWhiteId(entity.getEnrollWhiteId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollWhite0005", title = "报名白名单管理修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduStudentEnrollWhite0005",
            operationName = "报名白名单管理修改",
            dataType = "enrollWhiteId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentEnrollWhiteVO edit(EduStudentEnrollWhiteVO vo) {
        if (ObjectUtils.isEmpty(vo.getEnrollWhiteId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        Integer year = vo.getYear();
        EduStudentEnrollWhite oldEntity = eduStudentEnrollWhiteService.getById(vo.getEnrollWhiteId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        if (!year.equals(oldEntity.getYear())){
            throw new AppException("不能修改历史年份的数据！");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        long count = eduStudentEnrollWhiteService.count(Wrappers.<EduStudentEnrollWhite>lambdaQuery()
                .ne(EduStudentEnrollWhite::getEnrollWhiteId, vo.getEnrollWhiteId())
                .eq(EduStudentEnrollWhite::getYear, vo.getYear())
                .eq(EduStudentEnrollWhite::getSchoolId, vo.getSchoolId())
                .eq(EduStudentEnrollWhite::getIdcard, vo.getIdcard()));
        if (count > 0) {
            throw new AppException("不能将身份证修改为别人的身份证");
        }
        //保存数据
        EduStudentEnrollWhite entity = Convert.convert(EduStudentEnrollWhite.class, vo);
        eduStudentEnrollWhiteService.updateById(entity);

        //根据身份证和年份清除缓存
        clearPreBeforeCache(vo);
        return vo;
    }

    private void clearPreBeforeCache(EduStudentEnrollWhiteVO vo) {
        if (StringUtils.isEmpty(vo.getIdcard())){
            throw new AppException("传入的身份证不能为空!");
        }
        if (vo.getYear() == null){
            throw new AppException("传入的年份不能为空!");
        }
        QueryWrapper<EduStudentEnrollPre> preWrapper =  new QueryWrapper<>();
        preWrapper.lambda().select(EduStudentEnrollPre::getStudentEnrollPreId)
                .eq(EduStudentEnrollPre::getIdcard, vo.getIdcard())
                        .eq(EduStudentEnrollPre::getYear, vo.getYear());
        List<EduStudentEnrollPre> list = enrollPreService.list(preWrapper);
        for (EduStudentEnrollPre eduStudentEnrollPre : list) {
            eduEnrollClearCacheHelper.clearCachePreBefore(eduStudentEnrollPre.getStudentEnrollPreId());
        }
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollWhite0006", title = "报名白名单管理根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduStudentEnrollWhite0006",
            operationName = "报名白名单管理删除",
            dataType = "enrollWhiteId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduStudentEnrollWhiteKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getEnrollWhiteId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduStudentEnrollWhite entity = eduStudentEnrollWhiteService.getById(vo.getEnrollWhiteId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        EduStudentEnrollWhiteVO whitVO =  Convert.convert(EduStudentEnrollWhiteVO.class, entity);
        clearPreBeforeCache(whitVO);
        eduStudentEnrollWhiteService.removeById(vo.getEnrollWhiteId());
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollWhite0007", title = "校验白名单是否存在", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    public Boolean checkExist(EduStudentEnrollWhiteVO vo) {
        if (StringUtils.isBlank(vo.getIdcard())){
            throw new AppException("身份证不能为空");
        }
        if(StringUtils.isBlank(vo.getSchoolId())){
            throw new AppException("学校id不能为空");
        }
        if(ObjectUtils.isEmpty(vo.getYear())){
          throw new AppException("年份不能为空");
        }
        if(eduStudentEnrollWhiteService.count(Wrappers.<EduStudentEnrollWhite>lambdaQuery()
                .eq(EduStudentEnrollWhite::getYear, vo.getYear())
                .eq(EduStudentEnrollWhite::getAuthStatus,BoolEnum.TRUE.getType())
                .eq(EduStudentEnrollWhite::getIdcard, vo.getIdcard())
                .eq(EduStudentEnrollWhite::getSchoolId, vo.getSchoolId())) > 0){
            return true;
        }
        return false;
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollWhite0008", title = "白名单导入模板下载", funcType = FuncTypeEnum.other, checkPrivilege = BoolEnum.FALSE)
    public void downloadModel(ExcelVO vo, HttpServletResponse response) {
        Map<String, List<SelectVO>> dictMap = new HashMap<>();
        String areaCode = SessionUtil.getAreaCode();
        if (areaCode.equalsIgnoreCase("330700")){
            dictMap.put("schoolName", getSchoolDictMap(null));
        }else{
            dictMap.put("schoolName", getSchoolDictMap(areaCode));
        }
        vo.setDictMap(dictMap);
        excelIntegration.downloadModel(vo, response);
    }

    private List<SelectVO> getSchoolDictMap(String areaCode) {
        LambdaQueryWrapper<EduSchool> queryWrapper = new QueryWrapper<EduSchool>()
                .lambda()
                .select(EduSchool::getOrgId, EduSchool::getShortName)
                .eq(EduSchool::getRecruitStatus, "0")
                .eq(StringUtils.isNotBlank(areaCode),EduSchool::getAreaCode, areaCode)
                .ne(EduSchool::getSchoolCategory, SchoolCategoryEnum.KINDERGARTEN.getType());
        List<EduSchool> schoolList = eduSchoolService.list(queryWrapper);
        List<SelectVO> schoolDictList = new ArrayList<>();
        schoolList.forEach(
                item -> {
                    SelectVO selectVO = new SelectVO();
                    selectVO.setDictKey(item.getOrgId());
                    selectVO.setDictValue(item.getShortName());
                    selectVO.setDictField("schoolName");
                    selectVO.setFieldName("学校");
                    schoolDictList.add(selectVO);
                });
        return schoolDictList;
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollWhite0009", title = "白名单导入excel导入", funcType = FuncTypeEnum.insert)
    public ExcelReadVO readExcel(ExcelVO vo, MultipartFile file) {
        ExcelReadVO excelReadVO = new ExcelReadVO();
        excelReadVO.setCount(0);
        ExcelModelVO excelModelVO = this.getModel(vo);
        InputStream in = null;
        try {
            in = file.getInputStream();
            List<Map<String, Object>> list = excelCommonHelper.readExcel(excelModelVO, in, true);
            List<Map<String, Object>> successList = new ArrayList<>();
            List<Map<String, Object>> errorList = new ArrayList<>();
            //查询学校
            QueryWrapper<EduSchool> schoolQueryWrapper = new QueryWrapper<>();
            schoolQueryWrapper.lambda().ne(EduSchool::getSchoolCategory, SchoolCategoryEnum.KINDERGARTEN.getType());
            List<EduSchool> schoolList = eduSchoolService.list(schoolQueryWrapper);
            for (Map<String, Object> map : list) {
                EduStudentEnrollWhiteVO eduStudentEnrollWhiteVO = JSON.parseObject(JSON.toJSONString(map), EduStudentEnrollWhiteVO.class);
                String remarks = "";
                boolean flag = false;

                if (ObjectUtils.isEmpty(eduStudentEnrollWhiteVO.getIdcard())) {
                    remarks += "身份证不能为空！";
                    flag = true;
                }else {
                    eduStudentEnrollWhiteVO.setIdcard(eduStudentEnrollWhiteVO.getIdcard().trim().replaceAll("\\s+", ""));
                }
                if (ObjectUtils.isEmpty(eduStudentEnrollWhiteVO.getFullName())) {
                    remarks += "姓名不能为空！";
                    flag = true;
                }else {
                    //除去两端的空格和中间的空格
                    eduStudentEnrollWhiteVO.setFullName(eduStudentEnrollWhiteVO.getFullName().trim().replaceAll("\\s+", ""));
                }
                if (ObjectUtils.isEmpty(eduStudentEnrollWhiteVO.getSchoolName())) {
                    remarks += "学校名称不能为空！";
                    flag = true;
                }else {
                    AtomicBoolean schoolFlag = new AtomicBoolean(false);
                    schoolList.forEach(
                            school -> {
                                if (!ObjectUtils.isEmpty(eduStudentEnrollWhiteVO.getSchoolName())
                                        && school.getShortName()
                                        .replaceAll("（", "(")
                                        .replaceAll("）", ")")
                                        .equals(
                                                eduStudentEnrollWhiteVO.getSchoolName()
                                                        .replaceAll("（", "(")
                                                        .replaceAll("）", ")"))) {
                                    schoolFlag.set(true);
                                    eduStudentEnrollWhiteVO.setSchoolId(school.getOrgId());
                                    eduStudentEnrollWhiteVO.setSchoolName(school.getSchoolName());
                                    eduStudentEnrollWhiteVO.setAreaCode(school.getAreaCode());
                                }
                            });
                    if (!schoolFlag.get()) {
                        remarks += "找不到招生学校！";
                        flag = true;
                    }
                }
                if (ObjectUtils.isEmpty(eduStudentEnrollWhiteVO.getYear())) {
                    eduStudentEnrollWhiteVO.setYear(eduHelper.thisYear());
                }
                if (ObjectUtils.isEmpty(eduStudentEnrollWhiteVO.getAuthStatus())){
                    eduStudentEnrollWhiteVO.setAuthStatus("1");
                }
                long count = eduStudentEnrollWhiteService.count(Wrappers.<EduStudentEnrollWhite>lambdaQuery()
                        .eq(EduStudentEnrollWhite::getYear, eduStudentEnrollWhiteVO.getYear())
                        .eq(EduStudentEnrollWhite::getSchoolId, eduStudentEnrollWhiteVO.getSchoolId())
                        .eq(EduStudentEnrollWhite::getIdcard, eduStudentEnrollWhiteVO.getIdcard()));
                if (count > 0) {
                    remarks += "该学生已经在白名单了！";
                    flag = true;
                }

                if (flag) {
                    eduStudentEnrollWhiteVO.setExcelRemarks(remarks);
                    errorList.add(JSON.parseObject(JSON.toJSONString(eduStudentEnrollWhiteVO), Map.class));
                } else {
                    successList.add(JSON.parseObject(JSON.toJSONString(eduStudentEnrollWhiteVO), Map.class));
                }
            }
            excelReadVO.setCount(successList.size() + errorList.size());
            excelReadVO.setSuccessCount(successList.size());
            excelReadVO.setSuccessList(successList);
            excelReadVO.setErrorList(errorList);
            excelReadVO.setErrorCount(errorList.size());
        } catch (AppException appEx) {
            throw appEx;
        } catch (IOException e) {
            throw new AppException("读取文件异常:" + e.getMessage());
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return excelReadVO;
    }

    @Override
    @OpApi(funcCode = "eduStudentEnrollWhite0010", title = "白名单导入excel保存（PC）", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduStudentEnrollWhite0010",
            operationName = "白名单导入excel保存（PC）",
            dataType = "batch",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void addExcel(WhiteExcelSaveVO vo) {
        if (CollectionUtils.isEmpty(vo.getStudentsVOList())) {
            throw new AppException("没有传入需要保存的信息");
        }
        List<EduStudentEnrollWhiteVO> eduStudentEnrollWhiteVOS = Convert.toList(EduStudentEnrollWhiteVO.class, vo.getStudentsVOList());
        for (EduStudentEnrollWhiteVO eduStudentEnrollWhiteVO : eduStudentEnrollWhiteVOS) {
            this.add(eduStudentEnrollWhiteVO);
        }
    }

    public ExcelModelVO getModel(ExcelVO vo) {
        return this.getModel(vo.getModelCode(), vo.getExcelVersion(), vo.getDictMap());
    }

    public ExcelModelVO getModel(
            String modelCode, String version, Map<String, List<SelectVO>> dictMap) {
        if (StringUtils.isBlank(modelCode)) {
            throw new AppException("模板编号不能为空");
        }
        QueryWrapper<ExcelModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ExcelModel::getModelCode, modelCode);
        if (StringUtils.isBlank(version)) {
            queryWrapper.lambda().eq(ExcelModel::getNewFlag, BoolEnum.TRUE.getType());
        } else {
            queryWrapper.lambda().eq(ExcelModel::getExcelVersion, version);
        }
        ExcelModel excelModel = excelModelService.getOne(queryWrapper, false);
        if (excelModel == null) {
            throw new AppException("未找到excel模板信息");
        }
        ExcelModelVO excelModelVO = Convert.convert(ExcelModelVO.class, excelModel);
        // 获取字段信息
        List<ExcelParamVO> paramVOList = this.getByModelId(excelModelVO.getId());
        for (ExcelParamVO excelParamVO : paramVOList) {
            if (excelParamVO.getHeaderBgColor() == null) {
                excelParamVO.setHeaderBgColor(excelModelVO.getHeaderBgColor());
            }
            if (excelParamVO.getHeaderColor() == null) {
                excelParamVO.setHeaderColor(excelModelVO.getHeaderColor());
            }
            if (excelParamVO.getBodyBgColor() == null) {
                excelParamVO.setBodyBgColor(excelModelVO.getBodyBgColor());
            }
            if (excelParamVO.getBodyColor() == null) {
                excelParamVO.setBodyColor(excelModelVO.getBodyColor());
            }
            if (StringUtils.isBlank(excelParamVO.getWrapText())) {
                excelParamVO.setWrapText(excelModelVO.getWrapText());
            }
            if (StringUtils.isBlank(excelParamVO.getFontName())) {
                excelParamVO.setFontName(excelModelVO.getFontName());
            }
            if (excelParamVO.getFontSize() == null) {
                excelParamVO.setFontSize(excelModelVO.getFontSize());
            }
            if (excelParamVO.getBorderSize() == null) {
                excelParamVO.setBorderSize(excelModelVO.getBorderSize());
            }
            if (excelParamVO.getBorderColor() == null) {
                excelParamVO.setBorderColor(excelModelVO.getBorderColor());
            }
            if (ExcelFieldVO.CellTypeEnum.SELECT.getType().equals(excelParamVO.getCellType())
                    || ExcelFieldVO.CellTypeEnum.MULTIPLESELECT
                    .getType()
                    .equals(excelParamVO.getCellType())) {
                String dictField = excelParamVO.getDictField();
                if (StringUtils.isBlank(dictField)) {
                    dictField = excelParamVO.getKey();
                }
                if (dictMap != null && dictMap.containsKey(dictField)) {
                    excelParamVO.setCodeList(dictMap.get(dictField));
                } else {
                    List<SelectVO> selectList = sysDictService.getDictByDictField(dictField);
                    excelParamVO.setCodeList(selectList);
                }
            }
        }
        excelModelVO.setParamVOList(paramVOList);
        if (StringUtils.isBlank(excelModelVO.getExcelType())) {
            excelModelVO.setExcelType(ExcelTypeEnum.xls.toString());
        }
        if (StringUtils.isBlank(excelModelVO.getExcelMode())) {
            excelModelVO.setExcelMode("2");
        }
        return excelModelVO;
    }

    public List<ExcelParamVO> getByModelId(String excelModelId) {
        QueryWrapper<ExcelParam> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(ExcelParam::getExcelModelId, excelModelId)
                .orderByAsc(ExcelParam::getOrderNo)
                .orderByAsc(ExcelParam::getCreateTime);
        List<ExcelParam> list = excelParamService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<ExcelParamVO> voList = Convert.toList(ExcelParamVO.class, list);
        return voList;
    }

}
